package com.ui.basic.controller.admin;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.CommandRole;
import com.ui.basic.service.admin.RoleService;

@Controller
public class SetupRoleController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(SetupRoleController.class);

	@Autowired
	private RoleService roleService;

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	@RequestMapping(value = "/admin/role.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>SetupRoleController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "role.page";
	}

	@RequestMapping(value = "/admin/role.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandRole") CommandRole commandRole) {
		LOGGER.debug(">>>SetupRoleController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			model.addAttribute("commandRole", roleService.getRole(commandRole));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "role.view";
	}

	@RequestMapping(value = "/admin/role.form", method = RequestMethod.POST)
	public String edit1(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandRole") CommandRole commandRole) {
		LOGGER.debug(">>>SetupRoleController.edit1");
		return edit(model, request, response, commandRole);
	}
	
	@RequestMapping(value = "/admin/role.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandRole") CommandRole commandRole) {
		LOGGER.debug(">>>SetupRoleController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			commandRole.setMenuCollection(roleService.getMenuColl());

			if (commandRole.getId() != null)
				model.addAttribute("commandRole", roleService
						.getRoleDetail(commandRole));
			else {
				model.addAttribute("commandRole", commandRole);
				commandRole.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "role.edit";
	}

	@RequestMapping(value = "/admin/role.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandRole") CommandRole commandRole,
			BindingResult result, Model model) {
		LOGGER.debug(">>>SetupRoleController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandRole, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				roleService.save(commandRole);
				return view(model, request, response, commandRole);
			} catch (Exception e) {
				if (commandRole.getId() != null)
					sendError(response, getMessage(request,
							"error.roledata.saveFailed",
							new String[] { commandRole.getRoleName() }));
				else
					sendError(response, getMessage(request,
							"error.roledata.exist", new String[] { commandRole
									.getRoleName() }));
				return null;
			}
		}
		return null;
	}

	@RequestMapping(value = "/admin/role.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandRole") CommandRole commandRole,
			BindingResult result) {
		LOGGER.debug(">>>SetupRoleController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandRole.getId() != null
				|| (commandRole.getSelectedRecord() != null && commandRole
						.getSelectedRecord().length > 0)) {
			try {
				roleService.delete(commandRole);
				return view(model, request, response, commandRole);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}
}
